import javax.swing.*;

import a_obsolete.Bill;
import a_obsolete.TreatmentRecord;

import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.*;

/**
 * Allows users to view the invoice of their last appointment
 * 
 * @author Zach
 * @version 1.0
 * 
 */
public class ViewInvoice extends JPanel{
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	/**
	 * Field invoiceInfo.
	 */
	private ArrayList<String> invoiceInfo;
	/**
	 * Field total.
	 */
	double total = 200.00;
	/**
	 * Field lblPatientName.
	 */
	private JLabel lblPatientName;
	/**
	 * Field lblDateOfVisit.
	 */
	private JLabel lblDateOfVisit;
	/**
	 * Field lblDoctor.
	 */
	private JLabel lblDoctor;
	/**
	 * Field lblItemizedBill.
	 */
	private JLabel lblItemizedBill;
	/**
	 * Field dateLabel.
	 */
	private JLabel dateLabel;
	/**
	 * Field patientLabel.
	 */
	private JLabel patientLabel;
	/**
	 * Field doctorLabel.
	 */
	private JLabel doctorLabel;
	/**
	 * Field lblLabWork.
	 */
	private JLabel lblLabWork;
	/**
	 * Field lblNewLabel_1.
	 */
	private JLabel lblNewLabel_1;
	/**
	 * Field lblTotal.
	 */
	private JLabel lblTotal;
	/**
	 * Field lblNewLabel.
	 */
	private JLabel lblNewLabel;
	/**
	 * Field lwLabel.
	 */
	private JLabel lwLabel;
	/**
	 * Field presLabel.
	 */
	private JLabel presLabel;
	/**
	 * Field laborLabel.
	 */
	private JLabel laborLabel;
	/**
	 * Field label_2.
	 */
	private JLabel label_2;
	/**
	 * Field totalLabel.
	 */
	private JLabel totalLabel;
	/**
	 * Field presAmtLabel.
	 */
	private JLabel presAmtLabel;
	/**
	 * Field lwAmtLabel.
	 */
	private JLabel lwAmtLabel;
	/**
	 * Field patientId.
	 */
	private String patientId;
	/**
	 * Field btnClose.
	 */
	JButton btnClose;
	
	
	/**
	 * ViewInvoice Constructor
	 * @param main Reference to the main window
	 * @param ID the patient ID
	 * @param database Reference to the Database
	 */
	public ViewInvoice(MainWindow main, Database database, String ID){

		this.patientId = ID;
		this.main = main;
		this.database = database;
		
		main.showMessage("Here is your invoice from your last appointment.");
		
		setLayout(null);
		createLayout();
		calculateTotal();
		
	}
	
	/**
	 * Calculates the total amount due in their invoice
	 */
	private void calculateTotal(){
		try {
			invoiceInfo =database.patientInvoice(patientId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try{
		patientLabel.setText(invoiceInfo.get(0) + " " + invoiceInfo.get(1));
		dateLabel.setText(invoiceInfo.get(2));
		doctorLabel.setText(invoiceInfo.get(3));
		presLabel.setText(invoiceInfo.get(4));
		lwLabel.setText(invoiceInfo.get(5));
		
		
		
		if(invoiceInfo.get(4).equals("None")){
			presAmtLabel.setText("$0.00");
		} else {
			presAmtLabel.setText("$50.00");
			total += 50.00;
		}
		if(invoiceInfo.get(5).equals("None")){
			lwAmtLabel.setText("$0.00");
		} else {
			lwAmtLabel.setText("$150.00");
			total += 150.00;
		}
		totalLabel.setText("$"+total+"0");
		} catch (IndexOutOfBoundsException e){
			main.showError("No invoice is currently available");
		}
	}
	
	/**
	 * Removes the tab from the tabbed pane
	 */
	private void remove(){
		main.removeTab(this);
		main.clearMessage();
	}
	
	/**
	 * Creates the GUI layout
	 */
	private void createLayout(){
		lblPatientName = new JLabel("Patient Name:");
		lblPatientName.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblPatientName.setBounds(45, 32, 144, 14);
		add(lblPatientName);
		
		lblDateOfVisit = new JLabel("Date of Visit:");
		lblDateOfVisit.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblDateOfVisit.setBounds(45, 57, 86, 14);
		add(lblDateOfVisit);
		
		lblDoctor = new JLabel("Doctor:");
		lblDoctor.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblDoctor.setBounds(45, 82, 86, 14);
		add(lblDoctor);

		lblItemizedBill = new JLabel("Itemized Bill");
		lblItemizedBill.setFont(new Font("Lucida Grande", Font.BOLD, 14));
		lblItemizedBill.setBounds(45, 143, 192, 14);
		add(lblItemizedBill);
		
		dateLabel = new JLabel("");
		dateLabel.setBounds(212, 57, 238, 14);
		add(dateLabel);
		
		patientLabel = new JLabel("");
		patientLabel.setBounds(212, 32, 238, 14);
		add(patientLabel);
		
		doctorLabel = new JLabel("");
		doctorLabel.setBounds(212, 82, 238, 14);
		add(doctorLabel);
		
		lblLabWork = new JLabel("Lab Work:");
		lblLabWork.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblLabWork.setBounds(45, 189, 105, 14);
		add(lblLabWork);
		
		lblNewLabel = new JLabel("Prescriptions:");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblNewLabel.setBounds(45, 214, 105, 14);
		add(lblNewLabel);
		
		lblNewLabel_1 = new JLabel("Labor:");
		lblNewLabel_1.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblNewLabel_1.setBounds(45, 239, 46, 14);
		add(lblNewLabel_1);
		
		lblTotal = new JLabel("Total:");
		lblTotal.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		lblTotal.setBounds(45, 276, 46, 14);
		add(lblTotal);
		
		lwLabel = new JLabel("");
		lwLabel.setBounds(150, 189, 170, 14);
		add(lwLabel);
		
		presLabel = new JLabel("");
		presLabel.setBounds(150, 214, 170, 14);
		add(presLabel);
		
		laborLabel = new JLabel("$200.00");
		laborLabel.setBounds(332, 239, 118, 14);
		add(laborLabel);
		
		label_2 = new JLabel("");
		label_2.setBounds(137, 264, 118, 14);
		add(label_2);
		
		totalLabel = new JLabel("");
		totalLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
		totalLabel.setBounds(332, 276, 118, 14);
		add(totalLabel);
		
		presAmtLabel = new JLabel("$0.00");
		presAmtLabel.setBounds(332, 214, 118, 14);
		add(presAmtLabel);
		
	    lwAmtLabel = new JLabel("$0.00");
		lwAmtLabel.setBounds(332, 189, 118, 14);
		add(lwAmtLabel);
		
		ButtonListener bl = new ButtonListener();
		
		btnClose = new JButton("Close");
		btnClose.setBounds(273, 338, 152, 23);
		btnClose.addActionListener(bl);
		add(btnClose);
		
		JSeparator separator = new JSeparator();
		separator.setForeground(Color.DARK_GRAY);
		separator.setBounds(45, 165, 380, 12);
		add(separator);
		
		JSeparator separator_1 = new JSeparator();
		separator_1.setForeground(Color.DARK_GRAY);
		separator_1.setBounds(45, 253, 380, 12);
		add(separator_1);

		
	}
	
	/**
	 * Action Listener class to listen for button events
	 * 
	 * @author Zach
	 * @version 1.0
	 */
	private class ButtonListener implements ActionListener{


		/**
		 * Method actionPerformed.
		 * @param ae ActionEvent
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
		 */
		public void actionPerformed(ActionEvent ae) {
			
			if(ae.getSource().equals(btnClose)){
				remove();
				System.out.println("Removed");
			}
			
		}
		
	}
}